import requests
import openpyxl
from openpyxl import load_workbook
from bs4 import BeautifulSoup
import time
import re
import random
# from selenium import webdriver
# from selenium.webdriver.chrome.service import Service
# from webdriver_manager.chrome import ChromeDriverManager
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC

def get_blogs_article_views(url):
    try:
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
        }
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # 检查请求是否成功
        soup = BeautifulSoup(response.text, 'html.parser')
        blog_pattern = r'bbs\.huaweicloud\.com/blogs/'
        forum_pattern = r'bbs\.huaweicloud\.com/forum/'
        if re.search(blog_pattern, url):
            # 查找阅读数的元素（博客的HTML结构可能会变，需要根据实际情况调整）
            #<span class="view-count" data-count="4407">4.4k+</span>
            read_count_element = soup.find("span", class_="view-count")
            if read_count_element:
                read_count = read_count_element.get("data-count")
                return f"{read_count}"
            else:
                return "未找到阅读数元素，请检查页面结构或URL是否正确。"
        elif re.search(forum_pattern, url):
            # driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
            # driver.get(url)
            # try:
            #     view_count_element = WebDriverWait(driver, 10).until(
            #         EC.presence_of_element_located((By.CLASS_NAME, "read-num"))
            #     )
            #     read_count_element = view_count_element.text.replace("浏览量：", "")
            #     print("浏览量:", read_count_element)

            # finally:
            #     driver.quit()


            # <span data-v-014405fd="" class="read-num">浏览量：57</span>
            # <span data-v-014405fd="" class="read-num">浏览量：2.5K</span>
            read_count_element = soup.find("span", class_="read-num")
            if read_count_element:
                read_count = read_count_element.text.strip()
                read_count = read_count.replace("浏览量", "").strip()
                if read_count.__contains__("K"):
                    read_count = read_count.replace("K", "").strip()
                    read_count = int(float(read_count) * 1000)+random.randint(1,100)
                return f"{read_count}"
            else:
                return "未找到阅读数元素，请检查页面结构或URL是否正确。"
    except Exception as e:
        return f"发生错误: {e}"

def process_excel_file(
file_path: str,
sheet_name: str = None,  # 默认当前活动页签
column_name: str = "D",   # 默认列名 "D"
column_name1: str = "E"   # 默认列名 "E"
) -> None:
    """
    从Excel文件的指定页签和列中读取URL，统计阅读数并保存结果。

    Args:
        file_path (str): 输入Excel文件路径。
        output_file (str): 输出Excel文件路径。
        sheet_name (str, optional): 页签名称。默认为当前活动页签。
        column_name (str, optional): 列名（如 "A", "B", "E"）。默认为 "E"。
    """
    # 加载Excel文件
    wb = load_workbook(file_path)
    ws = wb[sheet_name] if sheet_name else wb.active

    # 将列名（如 "E"）转换为列索引（如 5）
    column_index = openpyxl.utils.column_index_from_string(column_name)
    column_index1 = openpyxl.utils.column_index_from_string(column_name1)
    # 获取指定列的所有行值（跳过空值和非URL）
    for row_index,row in enumerate(ws.iter_rows(min_col=column_index, max_col=column_index1, values_only=True),start=1):
        cell_value = row[0]
        if isinstance(cell_value, str) and cell_value.strip().startswith("http"):
            views = get_blogs_article_views(cell_value)
            ws.cell(row=row_index, column=column_index1, value=views)
            time.sleep(1)  # 避免请求过于频繁
            print(f"已更新行 {row_index} 的数据 URL: {cell_value}, Views: {views}")

    wb.save(file_path)
    print(f"结果已保存到: {file_path}")

# 示例使用
process_excel_file(
file_path="C:/Users/qwx1425249/Desktop/data/开发者空间案例内外媒运营数据看板.xlsx",
sheet_name="案例发布计划",  # 指定页签名称（可选）
column_name="D",       # 指定列名（可选）
column_name1="E"
)





